<?php

namespace App\Models;



use phpDocumentor\Reflection\Types\Boolean;

class Node extends Base
{
    //修改器 函数名有规定：route_name ->>>>> RouteName
    //入库之前干些事:如果route_name没设置，就用''
    //在修改和添加时会进修改器 create update
    public function setRouteNameAttribute($value)
    {
        $this->attributes['route_name'] = empty($value) ? '' : $value;
    }

    //访问器 my_menu ->>>>> MyMenu
    //对应blade.php模板中{{ $item->my_menu }}  $item是Node实例
    public function getMyMenuAttribute() {
        if ($this->is_menu == '1') {
            return '<span class="label label-success radius">是</span>';
        }
        return '<span class="label label-warning radius">否</span>';
    }

    //获取全部的数据
    public function getAllList() {
        $data = self::get()->toArray();
        return $this->treeLevel($data);
    }

    /**
     * @param array $arr_node_ids 用户拥有的节点id
     * @return array
     *
     */
    public function treeData($arr_node_ids) {
        $query = Node::where('is_menu', '1');
        if (auth()->user()->username != config('rbac.super')) {
            $query->whereIn('id', $arr_node_ids);
        }
        $menuData = $query->get()->toArray();

        return $this->subTree($menuData);
    }
}
